# -*- coding : "utf-8" -*-
# @姓名       : GUI
# @日期       : 2023/2/12 19:58
# @FileName  ： files.py
# @Software  ： PyCharm
import logging
import yaml

logger = logging.getLogger(__name__)


class YamlFile(dict):
    def __init__(self, path):
        super().__init__()  # 让对象按照原来方式完成实例化
        # 接下来完成自定义的代码
        self._path = path  # yaml文件路径
        self.load()  # 实例化时，自动加载yaml内容

    def load(self):
        with open(self._path, "r", encoding="utf-8") as f:
            data = yaml.safe_load(f)  # 字典

        if data:
            self.update(data)  # 两个字典内容进行合并

    def save(self):
        with open(self._path, "w", encoding="utf-8") as f:
            yaml.dump(dict(self), f, allow_unicode=True)
